package com.neusoft.nepa.controller;

import com.neusoft.nepa.UserLogin;
import com.neusoft.nepa.entity.Admin;
import com.neusoft.nepa.entity.Dept;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.util.StringConverter;
import javafx.util.converter.DefaultStringConverter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class DeptManagerViewController {
    @FXML
    private TableColumn<Dept,String> col_name;
    @FXML
    private TableColumn<Dept, String> col_code;
    @FXML
    private TableColumn<Dept, String> col_password;
    @FXML
    private TableView<Dept> tableview;
    private final ObservableList<Dept> DeptList = FXCollections.observableArrayList();
    private UserLogin mainApp;
    @FXML
    public void initialize() {
        col_name.setCellValueFactory(new PropertyValueFactory<>("Name"));
        col_code.setCellValueFactory(new PropertyValueFactory<>("Count"));
        col_password.setCellValueFactory(new PropertyValueFactory<>("description"));
        tableview.setItems(DeptList);

        // 启用表格编辑功能
        tableview.setEditable(true);

        // 设置用户名和密码列可编辑
        col_name.setCellFactory(TextFieldTableCell.forTableColumn(new DefaultStringConverter()));
        col_code.setCellFactory(TextFieldTableCell.forTableColumn(new DefaultStringConverter()));
        col_password.setCellFactory(TextFieldTableCell.forTableColumn(new DefaultStringConverter()));

        // 处理用户名列的编辑提交事件
        col_name.setOnEditCommit(event -> {
            Dept dept = event.getRowValue();
            String newValue = event.getNewValue();
            dept.setName(newValue);
            updateUserMapAndSave();
        });

        // 处理密码列的编辑提交事件
        col_code.setOnEditCommit(event -> {
            Dept dept = event.getRowValue();
            String newValue = event.getNewValue();
            dept.setCount(newValue);
            updateUserMapAndSave();
        });

        col_password.setOnEditCommit(event -> {
            Dept dept = event.getRowValue();
            String newValue = event.getNewValue();
            dept.setDescription(newValue);
            updateUserMapAndSave();
        });
    }
    private void updateUserMapAndSave() {
        ArrayList<Dept> ReturnDeptList = new ArrayList<>();
        ReturnDeptList.addAll(DeptList);
        try {
            this.mainApp.resetDeptList(ReturnDeptList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void queryDept() throws IOException {
        List<Dept> deptList = this.mainApp.DeptLoad();
        DeptList.setAll(deptList);
    }

    public void setMainApp(UserLogin mainApp) throws IOException {
        this.mainApp = mainApp;
        queryDept();
    }

    @FXML
    private void showUserManageView() throws IOException {
        this.mainApp.showUserManageView();
    }
    @FXML
    private void showDeptManageView() throws IOException {
        this.mainApp.showDeptManageView();
    }
    @FXML
    private void showColorMap() throws IOException {
        this.mainApp.showColorMap();
    }
    @FXML
    private void addDept() throws IOException {
        this.DeptList.add(new Dept("<UNK>","0","<UNK>"));
    }
}
